package cn.service.impl;

import cn.dao.HouseDao;
import cn.dao.impl.HouseDaoImpl;
import cn.entity.House;
import cn.entity.User;
import cn.service.HouseService;
import cn.utils.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class HouseServiceImpl implements HouseService {

    private HouseDao houseDao = new HouseDaoImpl();

    @Override
    public List<House> findHouses(Double price, String username, Date pubDate) {
        Transaction tx = null;
        List<House> houses = new ArrayList<House>();
        Calendar c = Calendar.getInstance();
        try {
            tx = HibernateUtil.getTransaction(HibernateUtil.currentSession());
            Date m = null;
            if (null!=pubDate){
                c.setTime(pubDate);
                c.add(Calendar.MONTH, -1);
                m = c.getTime();
            }
            houses = houseDao.findHouses(price,username,m);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return houses;
    }

    @Override
    public List<House> findByTitleAndFloo(String title, Integer floorage) {
        Transaction tx = null;
        List<House> houses = new ArrayList<House>();
        try {
            tx = HibernateUtil.getTransaction(HibernateUtil.currentSession());
            houses = houseDao.findByTitleAndFloo(title,floorage);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return houses;
    }
}
